package com.alibaba.csp.sentinel.dashboard.repository.metric.InfluxDb;


import com.influxdb.LogLevel;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.QueryApi;
import com.influxdb.client.WriteApiBlocking;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class InfluxDbConfig {

    @Value("${spring.influx.url:''}")
    private String influxDBUrl;

    @Value("${spring.influx.org:''}")
    private String org;

    @Value("${spring.influx.bucket:''}")
    private String bucket;

    @Value("${spring.influx.token:''}")
    private String token;

    public String getInfluxDBUrl() {
        return influxDBUrl;
    }

    public void setInfluxDBUrl(String influxDBUrl) {
        this.influxDBUrl = influxDBUrl;
    }

    public String getOrg() {
        return org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    /**
     * 初始化influx client
     * @return
     */
    @Bean
    public InfluxDBClient influxDBClient() {
        InfluxDBClient influxDBClient = InfluxDBClientFactory.create(influxDBUrl, token.toCharArray(), org,bucket);
        return influxDBClient;
    }

    /**
     * 创建Write Api （负责写入）
     * @param influxDBClient
     * @return
     */
    @Bean
    public WriteApiBlocking writeApiBlocking (InfluxDBClient influxDBClient) {
        WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking();
        return writeApiBlocking;
    }

    /**
     * 创建Query Api （负责查询）
     * @param influxDBClient
     * @return
     */
    @Bean
    public QueryApi queryApi(InfluxDBClient influxDBClient) {
        QueryApi queryApi = influxDBClient.getQueryApi();
        return queryApi;
    }

}